package com.yzmoney.wechat.service;

import com.yzmoney.common.constant.Constants;
import com.yzmoney.common.constant.PropertyUtils;
import com.yzmoney.common.model.JsonResult;
import com.yzmoney.common.util.HttpClientUtil;
import com.yzmoney.common.util.JDKStackTrace;
import com.yzmoney.common.util.JsonUtils;
import com.yzmoney.wechat.model.general.GeneralReturnCode;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 通讯录的行为操作
 * @author DOVE
 * 
 */
@Component
public class DepartmentService
{
	private static Logger logger = Logger.getLogger(DepartmentService.class);
	private static String baseQYHUserUrl = PropertyUtils.getValue("DEPATMENT_URL", "",
			Constants.QYH_PATH);

	@Autowired
	private WechatAccessTokenService wechatAccessTokenService;

	public DepartmentService()
	{
		super();
	}
	/**
	 * 创建和修改通讯录部门
	 * @param templateMsg json格式的部门信息
	 * @param operate 需要操作的用户行为 (添加>create，修改update)
	 * @return
	 */
	public JsonResult departmentOperate(String templateMsg,String operate)
	{
		String accessToken = wechatAccessTokenService.getQYHAccessToken();
		String url = getDepartmentUrl(accessToken,operate);
		JsonResult jsonResult = new JsonResult();
		if (StringUtils.isEmpty(url))
		{
			jsonResult.setCode(Constants.CODE_FAILURE);
			jsonResult.setMsg("请求发送模版地址为空，请联系管理员");
			return jsonResult;
		}
		String result = HttpClientUtil.post(url, templateMsg,"UTF-8");
		GeneralReturnCode generalReturnCode = JsonUtils.toObject(result, GeneralReturnCode.class);
		logger.info(Constants.LOGPRIFIX + "accessToken=" + accessToken + ",generalReturnCode="
				+ generalReturnCode.toString());

		if (generalReturnCode.getErrcode().equals("0"))
		{
			jsonResult.setCode(Constants.CODE_SUCCESS);

		} else
		{
			jsonResult.setCode(Constants.CODE_FAILURE);
			jsonResult.setMsg(generalReturnCode.getErrmsg());
		}
		return jsonResult;
	}
	/**
	 * 删除用户
	 * @param userId 用户编号
	 * @param operate 用户行为-删除
	 * @return
	 */
	public JsonResult userDelete(String userId,String operate){
		String accessToken = wechatAccessTokenService.getQYHAccessToken();
		String url = getDepartmentUrl(accessToken,operate);
		JsonResult jsonResult = new JsonResult();
		if (StringUtils.isEmpty(url))
		{
			jsonResult.setCode(Constants.CODE_FAILURE);
			jsonResult.setMsg("请求发送模版地址为空，请联系管理员");
			return jsonResult;
		}
		url=url+"&userid="+userId;
		try
		{
			String result = HttpClientUtil.get(url,null,"UTF-8");
			GeneralReturnCode generalReturnCode = JsonUtils.toObject(result, GeneralReturnCode.class);
			logger.info(Constants.LOGPRIFIX + "accessToken=" + accessToken + ",generalReturnCode="
					+ generalReturnCode.toString());

			if (generalReturnCode.getErrcode().equals("0"))
			{
				jsonResult.setCode(Constants.CODE_SUCCESS);

			} else
			{
				jsonResult.setCode(Constants.CODE_FAILURE);
				jsonResult.setMsg(generalReturnCode.getErrmsg());
			}
		} catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
			logger.debug(JDKStackTrace.getJDKStrack(e));
		}
		return jsonResult;
	}

	private String getDepartmentUrl(String accessToken,String action ){
		
		if(StringUtils.isEmpty(accessToken)){
			return null;
		}
		String url = baseQYHUserUrl+"/"+action+"?access_token="+accessToken;
		return url;
	}

}
